//
//  ViewController.m
//  03 -UIGestureRecognizer
//
//  Created by 402030900@qq.com on 16/7/18.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 手势识别器(7种)UIGestureRecognizer 所有手势的基类
    // 1.轻拍
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发(默认为1)
    tap.numberOfTapsRequired = 1;
    // 几个手指触发(按option - alt)(默认为1)
    tap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    // 2.长按
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始,默认0.5秒
    longPress.minimumPressDuration = 1;
    // 默认10像素,长按失败前,最大的移动距离
    longPress.allowableMovement = 1000;
       [self.view addGestureRecognizer:longPress];
    // 3.清扫
    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    // direction 方向可以设置多个,但行为要相同
    // 若行为不同,创建多个清扫手势
    // 左右与上下有冲突
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
//    [self.view addGestureRecognizer:swipe];
    // 练习:UIImageView 添加清扫换图
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 355, 647)];
    imageView.image = [UIImage imageNamed:@"01.jpg"];
//    [self.view addSubview:imageView];
    imageView.userInteractionEnabled = YES;
    UISwipeGestureRecognizer * changeSwipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeChangeImage:)];
    // 按位枚举,4个选项 -> 1111
    // 全选的情况  1(3) 1(2) 1(1) 1(0)
    changeSwipeRight.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
//    [imageView addGestureRecognizer:changeSwipeRight];
    UISwipeGestureRecognizer * changeSwipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeChangeImageLeft:)];
    changeSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//    [imageView addGestureRecognizer:changeSwipeLeft];
    
    // 4.平移
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//    [imageView addGestureRecognizer:pan];
    
    // 5.捏合
    UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
//    [imageView addGestureRecognizer:pinch];
    
    // 6.旋转
    UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    // 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer * screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePan:)];
    screenEdgePan.edges =UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    
    // Do any additional setup after loading the view, typically from a nib.
}
// 轻拍
-(void)tapAction :(UITapGestureRecognizer *)tap{
    NSLog(@"轻拍");
}
// 长按
-(void)longPressAction :(UITapGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog(@"长按结束");
    }

}
// 清扫
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{
    NSLog(@"清扫");
}
-(void)swipeChangeImage:(UISwipeGestureRecognizer *)swipe{
    NSLog(@"改变图片");
    NSLog(@"%ld",swipe.direction);
    UIImageView * imageView = (UIImageView *)swipe.view;
    imageView.image = [UIImage imageNamed:@"02.jpg"];
  
    if (swipe.direction & UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"1111");
    }if (swipe.direction & UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"2222");
    }
    
}
-(void)swipeChangeImageLeft:(UISwipeGestureRecognizer *)swipe{
    NSLog(@"改变图片");
    UIImageView * imageView = (UIImageView *)swipe.view;
    imageView.image = [UIImage imageNamed:@"01.jpg"];
    
}
// 平移
-(void)panAction:(UIPanGestureRecognizer *)pan{
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    }else if (pan.state == UIGestureRecognizerStateChanged){
        // 平移中
        // 平移手势,根据其他view上移动的过程,得到最重点
        CGPoint point = [pan translationInView:pan.view];
        // 根据Point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
    }else if (pan.state == UIGestureRecognizerStateEnded){
        // 平移结束
        pan.view.transform = CGAffineTransformIdentity;
    }
    NSLog(@"平移");
   
}
// 捏合
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    NSLog(@"%f",pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 设置缩放倍数为1
    pinch.scale = 1;
}
// 旋转
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //旋转弧度置为0
    rotation.rotation = 0;
}
-(void)screenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan{
    NSLog(@"屏幕边缘手势");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
